/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ws
 * Date: 2024-02-02
 * Time: 21:52
 *
 * https://www.nowcoder.com/share/jump/7745334641706882011957
 */
public class BM84 {
    public String longestCommonPrefix(String[] strs) {
        if (strs == null || strs.length == 0) {
            return "";
        }
        String res = strs[0];
        StringBuilder sb = new StringBuilder();
        for (int i = 1; i < strs.length; i++) {
            String cur = strs[i];
            int len = Math.min(res.length(), cur.length());
            for (int j = 0; j < len; j++) {
                if (res.charAt(j) == cur.charAt(j)) {
                    sb.append(res.charAt(j));
                } else {
                    break;
                }
            }
            res = sb.toString();
            sb.delete(0, sb.length());
        }
        return res;
    }
}
